package cn.com.wdjedu.androidapp.wjeduandroid.support.lib.global;


import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

import cn.com.wdjedu.androidapp.wjeduandroid.R;


/**
 * lyc
 */

public class GlobalContext extends Application {
    //singleton
    private static GlobalContext globalContext = null;
    public static final String EXTRA_LOGIN_INFO = "loginInfo"; /*登录信息intent字段*/

    private Activity activity;
    //正在跑的Activity
    private Activity currentRunningActivity;

    @Override
    public void onCreate() {
        super.onCreate();
        globalContext = this;
       // LeakCanary.install(this);

        //初始化配置ImageLoader图片加载框架
        initImageLoader();


    }

    /**
     * 初始化ImageLoader框架的对象,并设置
     */
    private void initImageLoader() {
        //默认的Options
        DisplayImageOptions dconfig = null;
        dconfig = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.ic_launcher)//图片加载过程中显示的默认图片
                .showImageOnFail(R.mipmap.ic_launcher)//加载图片出错显示的图片
                .cacheInMemory(true)//是否加载缓存
                .cacheOnDisk(true)//是否存储本地
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(0))//设置图片渐显的时间
                //.FadeInBitmapDisplayer()//设置图片渐显的时间
                // .SimpleBitmapDisplayer()//正常显示一张图片
                .displayer(new SimpleBitmapDisplayer())//设置正常显示一张图片
                .build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPoolSize(5)//设置线程数量一般1-5最好
                .denyCacheImageMultipleSizesInMemory()
                .threadPriority(Thread.MAX_PRIORITY - 2)//线程的优先级
                .tasksProcessingOrder(QueueProcessingType.FIFO) // default
                .memoryCacheExtraOptions(200, 200)//缓存的最大宽高
//                .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))//自定义内存缓存，如果出现内存溢出可以不添加
                .memoryCacheSize(50 * 1024 * 1024)//最大的缓存数量 机身的8分之一
                .discCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
                // .diskCacheFileCount(100)  // 可以缓存的文件数量
                .discCacheFileNameGenerator(new Md5FileNameGenerator())//加密
                .defaultDisplayImageOptions(dconfig)//设置默认情况下的Option
                .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000))
                .build(); // connectTimeout (5 s),
        ImageLoader
                .getInstance()
                .init(config);
    }

    /**
     * 单例对象
     */
    public static GlobalContext getInstance() {
        return globalContext;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    /**
     * 当前正在运行的Activity
     */
    public Activity getCurrentRunningActivity() {
        return currentRunningActivity;
    }

    /**
     * 设置当前正在运行的Activity
     */
    public void setCurrentRunningActivity(Activity currentRunningActivity) {
        this.currentRunningActivity = currentRunningActivity;
    }


    /**
     * 添加fragment到指定id容器
     *
     * @param manager         FragmentManager对象
     * @param containerViewId 加载fragment的容器id
     * @param fragment        加载的fragment对象
     */
    public void setContentByFragment(FragmentManager manager, int containerViewId,
                                     Fragment fragment) {
        String className = fragment.getClass().getCanonicalName();

        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(containerViewId, fragment, className);
        transaction.commit();
    }

    /**
     * 添加fragment到指定id容器
     *
     * @param manager         FragmentManager对象
     * @param containerViewId 加载fragment的容器id
     * @param fragment        加载的fragment对象
     */
    public void setContentByFragment(FragmentManager manager, int containerViewId,
                                     Fragment fragment,String tag) {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(containerViewId, fragment, tag);
        transaction.commit();
    }

}
